#include <bits/stdc++.h>
using namespace std;
#define int long long
void test()
{
    cout<<5+4%3;
}
void solve()
{
    int n,s,e,d,a,b,c;
    cin>>n>>s>>e>>d;
    if(s<=e)a=e-s;
    else a=e+n-s;
    int x=(s+d)%n,y=(s-d+n)%n;
    if(x<=e)b=e-x;
    else b=e+n-x;
    if(y<=e)c=e-y;
    else c=e+n-y;
    int ans=min({a,b,c});
    cout<<ans<<endl;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T=1;
    //cin>>T;
    while(T--)
    {
       solve();
       //test();
    }
    return 0;
}